/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.java.app.hacktouch.gesture

import android.app.Activity
import android.content.pm.ActivityInfo
import android.gesture.Gesture
import android.gesture.GestureOverlayView
import android.os.Bundle
import android.os.Environment
import android.util.Log
import android.view.MotionEvent
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.Spinner
import android.widget.Toast
import com.java.app.hacktouch.MainActivity
import com.java.app.hacktouch.R
import java.io.File

class CreateGestureActivity : Activity() {

    private var mGesture: Gesture? = null
    private var mDoneButton: View? = null
    private var spinner_num: Spinner? = null
    private var spinner_key_1: Spinner? = null
    private var spinner_key_2: Spinner? = null
    private var spinner_key_3: Spinner? = null
    private var spinner_key_4: Spinner? = null
    private var spinner_key_5: Spinner? = null
    private var spinner_key_6: Spinner? = null
    private var gesture_key_num = 0

    private val gestureName: String?
        get() {
            var gesture_name: String? = null
            if (gesture_key_num >= 1) {
                gesture_name = spinner_key_1!!.selectedItem.toString()
            }

            if (gesture_key_num >= 2) {
                gesture_name += "+"
                gesture_name += spinner_key_2!!.selectedItem.toString()
            }

            if (gesture_key_num >= 3) {
                gesture_name += "+"
                gesture_name += spinner_key_3!!.selectedItem.toString()
            }

            if (gesture_key_num >= 4) {
                gesture_name += "+"
                gesture_name += spinner_key_4!!.selectedItem.toString()
            }

            if (gesture_key_num >= 5) {
                gesture_name += "+"
                gesture_name += spinner_key_5!!.selectedItem.toString()
            }

            if (gesture_key_num >= 6) {
                gesture_name += "+"
                gesture_name += spinner_key_6!!.selectedItem.toString()
            }

            return gesture_name
        }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        requestedOrientation = ActivityInfo
                .SCREEN_ORIENTATION_LANDSCAPE //横屏

        setContentView(R.layout.create_gesture)

        mDoneButton = findViewById(R.id.done)

        InitSpinner()

        val overlay = findViewById<GestureOverlayView>(R.id.gestures_overlay)
        overlay.gestureStrokeType = GestureParams.strokeType
        overlay.gestureStrokeWidth = GestureParams.strokeWidth.toFloat()
        overlay.fadeOffset = GestureParams.fadeOffset
        overlay.gestureColor = GestureParams.color
        overlay.addOnGestureListener(GesturesProcessor())
    }

    private fun updateSpinner() {
        if (gesture_key_num >= 1) {
            spinner_key_1!!.visibility = View.VISIBLE
        } else {
            spinner_key_1!!.visibility = View.INVISIBLE
        }

        if (gesture_key_num >= 2) {
            spinner_key_2!!.visibility = View.VISIBLE
        } else {
            spinner_key_2!!.visibility = View.INVISIBLE
        }

        if (gesture_key_num >= 3) {
            spinner_key_3!!.visibility = View.VISIBLE
        } else {
            spinner_key_3!!.visibility = View.INVISIBLE
        }

        if (gesture_key_num >= 4) {
            spinner_key_4!!.visibility = View.VISIBLE
        } else {
            spinner_key_4!!.visibility = View.INVISIBLE
        }

        if (gesture_key_num >= 5) {
            spinner_key_5!!.visibility = View.VISIBLE
        } else {
            spinner_key_5!!.visibility = View.INVISIBLE
        }

        if (gesture_key_num >= 6) {
            spinner_key_6!!.visibility = View.VISIBLE
        } else {
            spinner_key_6!!.visibility = View.INVISIBLE
        }
    }

    private fun InitSpinner() {
        var adapter: ArrayAdapter<CharSequence>
        adapter = ArrayAdapter.createFromResource(this, R.array.gesture_num, android.R.layout.simple_spinner_item)
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        spinner_num = findViewById(R.id.gesture_num)
        spinner_num!!.adapter = adapter
        spinner_num!!.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {

            override fun onItemSelected(adapterView: AdapterView<*>, view: View, i: Int, l: Long) {
                Log.i(TAG, "context: " + adapterView.getItemAtPosition(i).toString())
                gesture_key_num = Integer.valueOf(adapterView.getItemAtPosition(i).toString())!!
                updateSpinner()
            }

            override fun onNothingSelected(adapterView: AdapterView<*>) {

            }
        }

        adapter = ArrayAdapter.createFromResource(this, R.array.gesture_id, android.R.layout.simple_spinner_item)
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        spinner_key_1 = findViewById(R.id.gesture_key_1)
        spinner_key_1!!.adapter = adapter
        spinner_key_1!!.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(adapterView: AdapterView<*>, view: View, i: Int, l: Long) {

            }

            override fun onNothingSelected(adapterView: AdapterView<*>) {

            }
        }

        spinner_key_2 = findViewById(R.id.gesture_key_2)
        spinner_key_2!!.adapter = adapter
        spinner_key_2!!.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(adapterView: AdapterView<*>, view: View, i: Int, l: Long) {

            }

            override fun onNothingSelected(adapterView: AdapterView<*>) {

            }
        }

        spinner_key_3 = findViewById(R.id.gesture_key_3)
        spinner_key_3!!.adapter = adapter
        spinner_key_3!!.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(adapterView: AdapterView<*>, view: View, i: Int, l: Long) {

            }

            override fun onNothingSelected(adapterView: AdapterView<*>) {

            }
        }

        spinner_key_4 = findViewById(R.id.gesture_key_4)
        spinner_key_4!!.adapter = adapter
        spinner_key_4!!.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(adapterView: AdapterView<*>, view: View, i: Int, l: Long) {

            }

            override fun onNothingSelected(adapterView: AdapterView<*>) {

            }
        }

        spinner_key_5 = findViewById(R.id.gesture_key_5)
        spinner_key_5!!.adapter = adapter
        spinner_key_5!!.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(adapterView: AdapterView<*>, view: View, i: Int, l: Long) {

            }

            override fun onNothingSelected(adapterView: AdapterView<*>) {

            }
        }

        spinner_key_6 = findViewById(R.id.gesture_key_6)
        spinner_key_6!!.adapter = adapter
        spinner_key_6!!.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(adapterView: AdapterView<*>, view: View, i: Int, l: Long) {

            }

            override fun onNothingSelected(adapterView: AdapterView<*>) {

            }
        }
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)

        if (mGesture != null) {
            outState.putParcelable("gesture", mGesture)
        }
    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle) {
        super.onRestoreInstanceState(savedInstanceState)

        mGesture = savedInstanceState.getParcelable("gesture")
        if (mGesture != null) {
            val overlay = findViewById<GestureOverlayView>(R.id.gestures_overlay)
            overlay.post { overlay.gesture = mGesture }

            mDoneButton!!.isEnabled = true
        }
    }

    fun addGesture(v: View) {
        if (mGesture != null) {
            val sStore = MainActivity.store
            val name = gestureName
            if (name == null) {
                Toast.makeText(this, getString(R.string.error_missing_name), Toast.LENGTH_LONG).show()
                return
            }
            Log.i(TAG, "add gesture name:" + name)
            sStore!!.addGesture(name, mGesture)
            sStore.save()

            setResult(Activity.RESULT_OK)

            val path = File(Environment.getExternalStorageDirectory(),
                    "gestures").absolutePath
            Toast.makeText(this, getString(R.string.save_success, path), Toast.LENGTH_LONG).show()
        } else {
            setResult(Activity.RESULT_CANCELED)
        }

        finish()
    }

    fun cancelGesture(v: View) {
        setResult(Activity.RESULT_CANCELED)
        finish()
    }

    private inner class GesturesProcessor : GestureOverlayView.OnGestureListener {
        override fun onGestureStarted(overlay: GestureOverlayView, event: MotionEvent) {
            mDoneButton!!.isEnabled = false
            mGesture = null
        }

        override fun onGesture(overlay: GestureOverlayView, event: MotionEvent) {}

        override fun onGestureEnded(overlay: GestureOverlayView, event: MotionEvent) {
            mGesture = overlay.gesture
            if (mGesture!!.length < LENGTH_THRESHOLD) {
                overlay.clear(false)
            }
            mDoneButton!!.isEnabled = true
        }

        override fun onGestureCancelled(overlay: GestureOverlayView, event: MotionEvent) {}
    }

    companion object {
        val TAG = "CreateGestureActivity"

        private val LENGTH_THRESHOLD = 120.0f
    }
}
